﻿<?php
class Model_Product extends Zend_Db_Table_Abstract
{
	protected $_name = 'product';
	protected $_primary = 'p_id';
	//protected $_primary ='cat_id';

	public function createProduct ($cat_id,$s_id,$p_ten,$p_gia,$p_soLuong,$p_daBan,$p_linkImg,$p_khuyenmai)
	{
		$rowProduct= $this->createRow();
		if ($rowProduct) {
			// update the row values
			$rowProduct->cat_id = $cat_id;
			$rowProduct->s_id = $s_id;
			$rowProduct->p_ten = $p_ten;
			$rowProduct->p_gia = $p_gia;				
			$rowProduct->p_soLuong = $p_soLuong;
			$rowProduct->p_daBan = $p_daBan;
			$rowProduct->p_linkImg = $p_linkImg;
			$rowProduct->p_khuyenmai = $p_khuyenmai;
			$rowProduct->save();			
			//return the new product			
			return $rowProduct;
		} else {
			throw new Zend_Exception("Không thể tạo sản phẩm này!!!");
		}
	}
		
	public static function getProductCategory($cat_id)
	{
		$productModel = new self();
		$select = $productModel->select();
		$select->order(array('p_id'))
			   ->where('cat_id =?',$cat_id);	
		return $productModel->fetchAll($select);
	}
	public static function getProductById($pr_id)
	{
		$productModel = new self();
		$select = $productModel->select();
		$select->order(array('p_id'))
			   ->where('p_id =?',$pr_id);	
		return $productModel->fetchAll($select);
	}
	
	public static function getProductSupplier($s_id)
	{
		$productModel = new self();
		$select = $productModel->select();
		$select->order(array('p_id'))
			   ->where('s_id =?',$s_id);	
		return $productModel->fetchAll($select);
	}	
	
	public static function getProductAll()
	{
		$productModel = new self();
		$select = $productModel->select();
		$select->order(array('cat_id'));	
		return $productModel->fetchAll($select);
	}	
    public function fetchLatest($count =6 )
    {
        return $this->fetchAll(null, 
            'p_id DESC', $count);
    }
	
	public function getProductbycost($giatu,$giaden)
	{       
			$productModel = new self();
            $select = $productModel->select();
			$select->order(array('cat_id'))
				   ->where('p_gia > ?', $giatu)
                   ->where('p_gia < ?',$giaden);
		    return $productModel->fetchAll($select);
	
	}
	public static function getProductCategoryexPid($cat_id,$id)
	{  
		$productModel = new self();
		$select = $productModel->select();
		$select->order(array('p_id'))
			   ->where("p_id > $id OR p_id < $id")
			   ->where('cat_id =?',$cat_id);
			   
		return $productModel->fetchAll($select);
	}

	public function getproductbyfind($giatu, $giaden, $var1, $var2)
	{        $productModel = new self();
            $select = $productModel->select();
			$select->order(array('cat_id'))
				   ->where('p_gia > ?', $giatu)
                   ->where('p_gia < ?',$giaden)
				   ->where('cat_id =?',$var1)
				   ->where('p_ten LIKE ?', '%'.$var2.'%');
		    return $productModel->fetchAll($select);
		
	}
	public function getproductbyfindnocat($giatu, $giaden, $var2)
	{        
			$productModel = new self();
            $select = $productModel->select();
			$select->order(array('cat_id'))
				   ->where('p_gia > ?', $giatu)
                   ->where('p_gia < ?',$giaden)
				   ->where('p_ten LIKE ?', '%'.$var2.'%');
		    return $productModel->fetchAll($select);
		
	}
	public static function getCatID()
	{
		$productModel = new self();
		$select = $productModel->select();
		$select->distinct()
		       ->from(array('p' => 'product'),
                    array('cat_id'));
		return $productModel->fetchAll($select);
	}	
	
	public function getproductbyfindnocatnoprice($var2)
	{        
			$productModel = new self();
            $select = $productModel->select();
			$select->order(array('cat_id'))
				   ->where('p_ten LIKE ?', '%'.$var2.'%');
		    return $productModel->fetchAll($select);
		
	}
    public function getproductbyfindnoprice($var1, $var2)
	{        $productModel = new self();
            $select = $productModel->select();
			$select->order(array('cat_id'))
				   
				   ->where('cat_id =?',$var1)
				   ->where('p_ten LIKE ?', '%'.$var2.'%');
		    return $productModel->fetchAll($select);
		
	}
	
	public function getProductbyHot()
	{
	  $count=6;
	  return $this->fetchAll(null, 
            'p_daBan DESC', $count);
		       
	}
	public function tryzenpaginator()
	{
	   $productModel = new self();
       $select = $productModel->select();
	   $select->order(array('cat_id'));
	  return $productModel->fetchAll($select);

	}
	
	public static function getProductLimit($start,$count)
	{
		$productModel = new self();
		$select = $productModel->select();
		$select->order(array('p_id'))
			   ->limit($start,$count);	
		return $productModel->fetchAll($select);
	}
             
	
}